import { WorldMgr } from "./core/ecs/WorldMgr";
import LabelObj from "./core/testEcs/LabelObj";
const { ccclass, property } = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {
    @property
    public createTime: number = 1000;
    @property({ type: cc.Prefab })
    public bullet = null;

    private _recTime: number = 0;

    public pools: cc.Node[] = []

    private get(): cc.Node {
        if (this.pools.length > 0) {
            return this.pools.pop();
        } else {
            let node = cc.instantiate(this.bullet);
            return node;
        }
    }
    private put(node) {
        this.pools.push(node);
    }

    onLoad() {

    }
    start() {
        this._recTime = this.createTime;
    }
    update(dt) {
        WorldMgr.ins().update()
        if (this._recTime > 0) {
            if (this._recTime > dt) {
                this._recTime -= dt;
            } else {
                let label: cc.Node = this.get();
                label.x = 0
                label.y = 0
                this.node.addChild(label)
                this._recTime = this.createTime;
            }
        }
        let childrens = this.node.children
        for (let label of childrens) {
            if (label.y >= this.node.height / 2) {
                label.destroy()
                this.put(label)
                this.node.removeChild(label)
            }
        }
    }
}
